﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-2-28 11:07:23
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 BRANDSHOW
    /// </summary>
    public class BRANDSHOW : IBRANDSHOW
    {
        public BRANDSHOW()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.BRANDSHOW model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@BS_CODE", SqlDbType.VarChar,6),
                    new SqlParameter("@BS_NAME", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_DESC1", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_DESC2", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_DESC3", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_DESC4", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_FILE1", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_FILE2", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_FILE3", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_FILE4", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_HTML1", SqlDbType.Text),
                    new SqlParameter("@BS_HTML2", SqlDbType.Text),
                    new SqlParameter("@BS_HTML3", SqlDbType.Text),
                    new SqlParameter("@BS_HTML4", SqlDbType.Text),
                    new SqlParameter("@BS_FIELD1", SqlDbType.VarChar,20),
                    new SqlParameter("@BS_FIELD2", SqlDbType.VarChar,20)
            };
            parameters[0].Value = model.BS_CODE;

            if (model.BS_NAME != null)
                parameters[1].Value = model.BS_NAME;
            else
                parameters[1].Value = DBNull.Value;


            if (model.BS_DESC1 != null)
                parameters[2].Value = model.BS_DESC1;
            else
                parameters[2].Value = DBNull.Value;


            if (model.BS_DESC2 != null)
                parameters[3].Value = model.BS_DESC2;
            else
                parameters[3].Value = DBNull.Value;


            if (model.BS_DESC3 != null)
                parameters[4].Value = model.BS_DESC3;
            else
                parameters[4].Value = DBNull.Value;


            if (model.BS_DESC4 != null)
                parameters[5].Value = model.BS_DESC4;
            else
                parameters[5].Value = DBNull.Value;


            if (model.BS_FILE1 != null)
                parameters[6].Value = model.BS_FILE1;
            else
                parameters[6].Value = DBNull.Value;


            if (model.BS_FILE2 != null)
                parameters[7].Value = model.BS_FILE2;
            else
                parameters[7].Value = DBNull.Value;


            if (model.BS_FILE3 != null)
                parameters[8].Value = model.BS_FILE3;
            else
                parameters[8].Value = DBNull.Value;


            if (model.BS_FILE4 != null)
                parameters[9].Value = model.BS_FILE4;
            else
                parameters[9].Value = DBNull.Value;


            if (model.BS_HTML1 != null)
                parameters[10].Value = model.BS_HTML1;
            else
                parameters[10].Value = DBNull.Value;


            if (model.BS_HTML2 != null)
                parameters[11].Value = model.BS_HTML2;
            else
                parameters[11].Value = DBNull.Value;


            if (model.BS_HTML3 != null)
                parameters[12].Value = model.BS_HTML3;
            else
                parameters[12].Value = DBNull.Value;


            if (model.BS_HTML4 != null)
                parameters[13].Value = model.BS_HTML4;
            else
                parameters[13].Value = DBNull.Value;


            if (model.BS_FIELD1 != null)
                parameters[14].Value = model.BS_FIELD1;
            else
                parameters[14].Value = DBNull.Value;


            if (model.BS_FIELD2 != null)
                parameters[15].Value = model.BS_FIELD2;
            else
                parameters[15].Value = DBNull.Value;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_BRANDSHOW_ADD", parameters, out rowsAffected);

          
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.BRANDSHOW model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@BS_CODE", SqlDbType.VarChar,6),
                    new SqlParameter("@BS_NAME", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_DESC1", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_DESC2", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_DESC3", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_DESC4", SqlDbType.VarChar,500),
                    new SqlParameter("@BS_FILE1", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_FILE2", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_FILE3", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_FILE4", SqlDbType.VarChar,200),
                    new SqlParameter("@BS_HTML1", SqlDbType.Text),
                    new SqlParameter("@BS_HTML2", SqlDbType.Text),
                    new SqlParameter("@BS_HTML3", SqlDbType.Text),
                    new SqlParameter("@BS_HTML4", SqlDbType.Text),
                    new SqlParameter("@BS_FIELD1", SqlDbType.VarChar,20),
                    new SqlParameter("@BS_FIELD2", SqlDbType.VarChar,20)
            };
            parameters[0].Value = model.BS_CODE;

            if (model.BS_NAME != null)
                parameters[1].Value = model.BS_NAME;
            else
                parameters[1].Value = DBNull.Value;


            if (model.BS_DESC1 != null)
                parameters[2].Value = model.BS_DESC1;
            else
                parameters[2].Value = DBNull.Value;


            if (model.BS_DESC2 != null)
                parameters[3].Value = model.BS_DESC2;
            else
                parameters[3].Value = DBNull.Value;


            if (model.BS_DESC3 != null)
                parameters[4].Value = model.BS_DESC3;
            else
                parameters[4].Value = DBNull.Value;


            if (model.BS_DESC4 != null)
                parameters[5].Value = model.BS_DESC4;
            else
                parameters[5].Value = DBNull.Value;


            if (model.BS_FILE1 != null)
                parameters[6].Value = model.BS_FILE1;
            else
                parameters[6].Value = DBNull.Value;


            if (model.BS_FILE2 != null)
                parameters[7].Value = model.BS_FILE2;
            else
                parameters[7].Value = DBNull.Value;


            if (model.BS_FILE3 != null)
                parameters[8].Value = model.BS_FILE3;
            else
                parameters[8].Value = DBNull.Value;


            if (model.BS_FILE4 != null)
                parameters[9].Value = model.BS_FILE4;
            else
                parameters[9].Value = DBNull.Value;


            if (model.BS_HTML1 != null)
                parameters[10].Value = model.BS_HTML1;
            else
                parameters[10].Value = DBNull.Value;


            if (model.BS_HTML2 != null)
                parameters[11].Value = model.BS_HTML2;
            else
                parameters[11].Value = DBNull.Value;


            if (model.BS_HTML3 != null)
                parameters[12].Value = model.BS_HTML3;
            else
                parameters[12].Value = DBNull.Value;


            if (model.BS_HTML4 != null)
                parameters[13].Value = model.BS_HTML4;
            else
                parameters[13].Value = DBNull.Value;


            if (model.BS_FIELD1 != null)
                parameters[14].Value = model.BS_FIELD1;
            else
                parameters[14].Value = DBNull.Value;


            if (model.BS_FIELD2 != null)
                parameters[15].Value = model.BS_FIELD2;
            else
                parameters[15].Value = DBNull.Value;


            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_BRANDSHOW_Update", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string BS_CODE)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@BS_CODE", SqlDbType.VarChar,6)};
            parameters[0].Value = BS_CODE;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_BRANDSHOW_Delete", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string BS_CODE)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@BS_CODE", SqlDbType.VarChar,6)};
            parameters[0].Value = BS_CODE;
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_BRANDSHOW_Exists", parameters, out rowsAffected) == 1;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.BRANDSHOW GetModel(string BS_CODE)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@BS_CODE", SqlDbType.VarChar,6)};
            parameters[0].Value = BS_CODE;
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_BRANDSHOW_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public IList<Model.BRANDSHOW> GetList()
        {
            SqlParameter[] parameters ={ };
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_BRANDSHOW_GetList", parameters, "ds");
            return GetList(ds);
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.BRANDSHOW> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[BRANDSHOW]", null, pageSize, pageIndex, fldSort, sort, strCondition, "BS_CODE", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.BRANDSHOW GetModel(DataRow r)
        {
            Model.BRANDSHOW model = new Model.BRANDSHOW();
            model.BS_CODE = SqlHelper.GetString(r["BS_CODE"]);
            model.BS_NAME = SqlHelper.GetString(r["BS_NAME"]);
            model.BS_DESC1 = SqlHelper.GetString(r["BS_DESC1"]);
            model.BS_DESC2 = SqlHelper.GetString(r["BS_DESC2"]);
            model.BS_DESC3 = SqlHelper.GetString(r["BS_DESC3"]);
            model.BS_DESC4 = SqlHelper.GetString(r["BS_DESC4"]);
            model.BS_FILE1 = SqlHelper.GetString(r["BS_FILE1"]);
            model.BS_FILE2 = SqlHelper.GetString(r["BS_FILE2"]);
            model.BS_FILE3 = SqlHelper.GetString(r["BS_FILE3"]);
            model.BS_FILE4 = SqlHelper.GetString(r["BS_FILE4"]);
            model.BS_HTML1 = SqlHelper.GetString(r["BS_HTML1"]);
            model.BS_HTML2 = SqlHelper.GetString(r["BS_HTML2"]);
            model.BS_HTML3 = SqlHelper.GetString(r["BS_HTML3"]);
            model.BS_HTML4 = SqlHelper.GetString(r["BS_HTML4"]);
            model.BS_FIELD1 = SqlHelper.GetString(r["BS_FIELD1"]);
            model.BS_FIELD2 = SqlHelper.GetString(r["BS_FIELD2"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.BRANDSHOW> GetList(DataSet ds)
        {
            List<Model.BRANDSHOW> l = new List<Model.BRANDSHOW>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
